package com.xb.controller;

import com.xb.entity.Meeting;
import com.xb.service.MeetingService;
import com.xb.utils.PageEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/meeting/*")
public class MeetingController extends BaseController{
    private MeetingService meetingService =new MeetingService();

    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 参加？取消会议
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void joinMeeting(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long meetingId = Long.parseLong(req.getParameter("meetingId"));
        Boolean flag = meetingService.isJoinMeeting(loginUser.getId(), meetingId);

        if (flag) {
            // 来取消会议的
            meetingService.delJoinMeeting(loginUser.getId(), meetingId);
        } else {

            // 来参加会议的
            meetingService.joinMeeting(loginUser.getId(), meetingId);
        }

        // 刷新会议详情
        resp.sendRedirect("/meeting/meetingDetail?meetingId=" + meetingId);
    }

        /**
         * 查询会议详情
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void meetingDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> param = getParam(req);
        // 1. meeting对象
        Long meetingId = Long.parseLong(param.get("meetingId"));
        Meeting meeting = meetingService.findById(meetingId);

        //应到人数
        String makeUser = meeting.getMakeUser();
        Integer joinCount = 1;

        if (makeUser.length() != 1) {
            // 1,2,3,4 => [1,2,3,4]
            joinCount = makeUser.split(",").length;
        }
        //实到人数
        Integer realCount = meetingService.findRealCount(meetingId);
        //未到人数
        Integer noCount = joinCount - realCount;
        // 我是否已经参加了本次会议
        Boolean isJoinMeeting = meetingService.isJoinMeeting(loginUser.getId(), meetingId);
        // 我是是否能操作本次会议
        List<String> ids = Arrays.asList(meeting.getMakeUser().split(","));
        Boolean isJoin = ids.contains(loginUser.getId() + "");

        req.setAttribute("meeting", meeting);
        req.setAttribute("joinCount", joinCount);
        req.setAttribute("realCount", realCount);
        req.setAttribute("noCount", noCount);
        req.setAttribute("isJoinMeeting", isJoinMeeting);
        req.setAttribute("isJoin", isJoin);

        req.getRequestDispatcher("/html/meeting_detail.jsp").forward(req, resp);


    }
        /**
         * 发布会议
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf8");
            // 获取前端提交的所有参数
            Map<String, String> param = getParam(req);
            // 将参数封装为一个Meeting对象
            Meeting meeting = new Meeting();

            meeting.setTitle(param.get("title"));
            meeting.setContent(param.get("content"));
            meeting.setDeptId(Long.parseLong(param.get("deptId")));
            meeting.setDeptName(param.get("deptName"));
            meeting.setStartTime(sdf.parse(param.get("startTime")));
            meeting.setEndTime(sdf.parse(param.get("endTime")));

            // 抄送的用户
            String makeUser = param.get("makeUser");

            if (makeUser.startsWith("[")) {
                // 代表前端抄送了多个用户[1, 2, 3, 4]
                makeUser = makeUser.substring(1);   // 1, 2, 3, 4]
                makeUser = makeUser.substring(0, makeUser.length() - 1);   // 1, 2, 3, 4
                makeUser = makeUser.replaceAll(" ", "");     // 1,2,3,4

            }

            // 设置到meeting对象中
            meeting.setMakeUser(makeUser);
            meeting.setPublishDate(new Date());     // 发布时间默认为当前时间
            meeting.setStatus(0L);      // 默认是未开始状态

            // 保存会议
            meetingService.save(meeting);

            // 查询最新分页信息
            resp.sendRedirect("/meeting/findPage?currPage=1");

        } catch (ParseException e) {
            throw new RuntimeException(e);
        }




    }

        /**
         * 会议搜索+分页
         * @param req
         * @param resp
         * @throws ServletException
         * @throws IOException
         */
    protected void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String> param = getParam(req);

        // 当前页码
        Integer currPage = Integer.parseInt(param.get("currPage"));

        // 搜索的条件-title
        String title = param.get("title");

        // 搜索条件-status
        String status = param.get("status");

        // 查询分页
        PageEntity<Meeting> pageData = meetingService.findPage(title, status, currPage);

        req.setAttribute("pageData", pageData);
        req.setAttribute("title", title);
        req.setAttribute("status", status);

        req.getRequestDispatcher("/html/meeting.jsp").forward(req, resp);

    }

    }
